//
//  WmInputView.swift
//  Wmsmobile
//
//  Created by Nick xu on 16/8/4.
//  Copyright © 2016年 hddden. All rights reserved.
//

import UIKit
import SnapKit

class WmInputView: UIView , UITextFieldDelegate{
    //MARK: - property
   
    lazy var textField:customTextfield = {
        let tf = customTextfield()
        tf.keyboardType = UIKeyboardType.numbersAndPunctuation
        tf.placeholder = "输入/扫描"
        tf.inputView = customTextfield.emptyView
        tf.font = UIFont.systemFont(ofSize: (WMSFonts["founctionCell"]!)[userInfo!.fontLevel.rawValue])
        tf.textColor = UIColor.hexStringToUIColor("#666666")
        tf.tintColor = UIColor.hexStringToUIColor("#00acff")
        tf.leftViewMode = UITextFieldViewMode.always
        tf.delegate = self
        return tf
    }()
    lazy var button_camera:UIButton = {
        let button = UIButton()
        button.imageView?.tintColor = UIColor.hexStringToUIColor("#0ebfb1")
        button.setImage(UIImage(named: "camera"), for: UIControlState())
        return button
    }()
    lazy var button_clear:UIButton = {
        let button = UIButton()
        button.setImage(UIImage(named: "Cancel"), for: UIControlState())
        return button
    }()
    //MARK: - life cycle
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.setUpUI()
    }
    convenience init(frame: CGRect , cameraAble: Bool) {
        self.init(frame: frame)
        if !cameraAble{
            button_camera.snp.remakeConstraints({ (make) in
                make.centerY.equalTo(self)
                make.trailing.equalTo(self).offset(0)
                make.height.equalTo(0)
                make.width.equalTo(0)
            })
        }
    }
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        self.setUpUI()
    }
//    override func willMoveToSuperview(newSuperview: UIView?) {
//        if captuvoIsOn{
//            button_camera.hidden = true
//        }else{
//            button_camera.hidden = false
//        }
//    }
    //MARK: - private
    fileprivate func setUpUI(){
//        self.layer.borderWidth = 0.7//把1修改为0.5
//        self.layer.borderColor = UIColor.hexStringToUIColor("#0ebfb1").CGColor
        self.addSubview(textField)
        self.addSubview(button_camera)
        self.addSubview(button_clear)
        button_clear.isHidden = true
        button_camera.addTarget(self, action: #selector(WmInputView.clickButton), for: UIControlEvents.touchUpInside)
        button_clear.addTarget(self, action: #selector(WmInputView.clickClearButton), for: UIControlEvents.touchUpInside)
        button_camera.snp.updateConstraints { (make) in  //添加约束
            make.centerY.equalTo(self)
            make.trailing.equalTo(self).offset(5)
            make.height.equalTo(80)
            make.width.equalTo(80)
        }
        button_clear.snp.updateConstraints { (make) in
            make.centerY.equalTo(self)
            make.trailing.equalTo(self).offset(5)
            make.height.equalTo(50)
            make.width.equalTo(50)
        }
        textField.snp.updateConstraints { (make) in
            make.centerY.equalTo(self)
            make.leading.equalTo(self).offset(-10)
            make.trailing.equalTo(self)
            make.height.equalTo(25)
        }
    }
    //MARK: - IBAction
    func clickButton(){
//      let vc = self.parentController()
        self.textField.becomeFirstResponder()
        self.textField.resignFirstResponder()
        self.textField.inputView = UIView(frame: CGRect(x: 0, y: 0, width: 0.5, height: 0.5))
       AVQRCodeViewManager.showComera()
    }
    func clickClearButton(){
        textField.text = ""
        button_clear.isHidden = true
        button_camera.isHidden = false
    }
}

class customTextfield: UITextField {
    static let emptyView = UIView(frame: CGRect(x: 0, y: 0, width: 0, height: 0.1))
    override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
        if self.point(inside: point, with: event) && self.inputView != nil {
            self.resignFirstResponder()
            self.inputView = nil
            self.becomeFirstResponder()
            return self
        }else{
           return super.hitTest(point, with: event)
        }
    }

}


